const db = require('../db/index.js')
exports.getCityList = async function(req, res) {
    const util = require('util');
    const query = util.promisify(db.query).bind(db);
    // 执行 SQL 语句失败
    let obj = {
        meta: {
            msg: "",
            status: ''
        },
        data: {}
    }

    let { query: searchName, pagenum, pagesize } = req.query
    pagenum = Number(pagenum);
    pagesize = Number(pagesize);
    console.log(pagenum)
    console.log(pagesize)
    let sql1; //查询语句1 查询总条数
    let sql2; //查询语句2 查询符合条件的
    let sqlerr = '' //存储数据库查询错误信息
        //总条数查询
    if (searchName) {
        sql1 = `select * from wm_city where city_name='${searchName}'`;
    } else {
        sql1 = `select * from wm_city`;
    }
    //分页结果查询
    let startPage = (pagenum - 1) * pagesize
    console.log(searchName)
    if (searchName) {
        console.log(searchName)
        sql2 = `select * from wm_city where city_name='${searchName}' limit ${startPage},${pagesize}`;
        console.log(sql2)
    } else {
        sql2 = `select * from wm_city  limit ${startPage},${pagesize}`;
    }

    try {
        let results = await query(sql1)
        obj.data.total = results.length;
        obj.data.pagenum = pagenum;

    } catch (err) {
        sqlerr = err;
    }

    try {
        let results = await query(sql2)
        obj.data.goods = results;
    } catch (err) {
        sqlerr = err;
    }
    // console.log(obj)

    // //整理需要传送给前端的数据
    if (sqlerr) {
        console.log(sqlerr)
        obj.meta.msg = sqlerr;
        obj.meta.status = 500;
        return res.send(obj)
    } else {
        obj.meta.msg = "获取城市列表成功";
        obj.meta.status = 200
        return res.send(obj)
    }

}


exports.searchCityById = (req, res) => {
    let id = req.params.id;
    let sql1 = 'select * from wm_city where city_id = ?'
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(sql1, id, (err, results) => {
            if (err) {
                console.log(err);
                obj.meta.status = 500;
                obj.meta.msg = '数据库查询错误';
                return res.send(obj);
            } else {
                if (results.length === 1) {
                    var data1 = {
                        "city_id": results[0].city_id,
                        "city_name": results[0].city_name,
                        "city_num": results[0].city_num,
                    }
                    obj.meta.msg = '查询成功';
                    obj.meta.status = 200;
                    obj.data = data1;
                    return res.send(obj)
                }
            }
        })
        //根据id查询sp_manager,满足条件返回给前端

}


exports.editCityById = (req, res) => {
    let id = req.params.id;
    let { cityname, citynum } = req.body;
    let changeValue = {
        city_name: cityname,
        city_num: citynum
    }
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    let sql1 = 'update wm_city set ? where city_id=?';
    db.query(sql1, [changeValue, id], (err, results) => {
        if (err) {
            console.log(err);
            obj.meta.status = 500;
            obj.meta.msg = '数据库查询错误';
            return res.send(obj);
        } else {
            if (results.affectedRows == 1) {
                obj.meta.status = 200;
                obj.meta.msg = '修改信息成功';
                return res.send(obj);
            }
        }
    })

}